package cn.kgc.service.Impl;

import cn.kgc.dto.AppInfoVo;
import cn.kgc.dto.QueryCondition;
import cn.kgc.mapper.AppCategoryMapper;
import cn.kgc.mapper.AppInfoMapper;
import cn.kgc.mapper.AppVersionMapper;
import cn.kgc.mapper.DataDictionaryMapper;
import cn.kgc.pojo.AppCategory;
import cn.kgc.pojo.AppInfo;
import cn.kgc.pojo.AppVersion;
import cn.kgc.pojo.DataDictionary;
import cn.kgc.service.AppInfoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;


import java.util.ArrayList;
import java.util.List;

@Service("appInfoService")
public class AppInfoServiceImpl implements AppInfoService {

    @Autowired
    private AppInfoMapper appInfoMapper;
    @Autowired
    private DataDictionaryMapper dataDictionaryMapper;
    @Autowired
    private AppCategoryMapper appCategoryMapper;
    @Autowired
    private AppVersionMapper appVersionMapper;

    @Override
    public PageInfo<AppInfoVo> getAppListByPageId(QueryCondition queryCondition, Integer devId, Integer pageId) {
        //开启分页查询
        PageHelper.startPage(pageId,5);
        //封装查询条件
        Example example = new Example(AppInfo.class);
        if(queryCondition==null){
            example.and().andLike("softwareName",!StringUtils.isEmpty(queryCondition.getQuerySoftwareName())?"%"+queryCondition.getQuerySoftwareName()+"%":null)
                    .andEqualTo("status",queryCondition.getQueryStatus())
                    .andEqualTo("flatformId",queryCondition.getQueryFlatformId())
                    .andEqualTo("categoryLevel1",queryCondition.getQueryCategoryLevel1())
                    .andEqualTo("categoryLevel2",queryCondition.getQueryCategoryLevel2())
                    .andEqualTo("categoryLevel3",queryCondition.getQueryCategoryLevel3());
        }
        example.and().andEqualTo("devId",devId);
        //执行查询
        Page<AppInfo> appInfoList = (Page<AppInfo>) appInfoMapper.selectByExample(example);
        //创建新的page对象
        Page<AppInfoVo> voList = new Page<>();
        //将有有的分页信息拷贝到新的page对象中
        BeanUtils.copyProperties(appInfoList,voList);
        //遍历列表,将每个vo对象中的名称属性填上
        for(AppInfo appInfo : appInfoList){
            //准备vo对象
            AppInfoVo vo = new AppInfoVo();
            //对象属性拷贝
            BeanUtils.copyProperties(appInfo,vo);
            //设置补充的name属性
            vo.setFlatformName(findDictionary("APP_FLATFORM",appInfo.getFlatformId()).getValueName());
            vo.setStatusName(findDictionary("APP_STATUS",appInfo.getFlatformId()).getValueName());
            vo.setCategoryLevel1Name(findCateById(appInfo.getCategoryLevel1()).getCategoryName());
            vo.setCategoryLevel2Name(findCateById(appInfo.getCategoryLevel2()).getCategoryName());
            vo.setCategoryLevel3Name(findCateById(appInfo.getCategoryLevel3()).getCategoryName());
            if (appInfo.getVersionId() != null){
                vo.setVersionName(findVersionById(appInfo.getVersionId()).getVersionNo());
        }

            //将vo对象添加到最终的结果列表中
            voList.add(vo);
        }
        //返回分页对象
        return new PageInfo<>(voList);
    }

    private DataDictionary findDictionary(String typeCode,Integer valueId){
        DataDictionary dictionary = new DataDictionary();
        dictionary.setTypeCode(typeCode);
        dictionary.setValueId(valueId);
        return dataDictionaryMapper.selectOne(dictionary);
    }

    private AppCategory findCateById(Integer cid){
        return appCategoryMapper.selectByPrimaryKey(cid);
    }

    private AppVersion findVersionById(Integer versionId){
        return appVersionMapper.selectByPrimaryKey(versionId);
    }

    @Override
    public boolean addAppInfo(AppInfo appInfo) {
        return appInfoMapper.insertSelective( appInfo) > 0;
    }
}
